<?php
/**
 * 政策协议控制器
 */

namespace app\admin\controller;

use think\Request;
use app\common\model\PolicyAgreement;

use app\common\validate\PolicyAgreementValidate;
use app\admin\logic\{
    PolicyAgreementLogic
};
class PolicyAgreementController extends Controller
{

   /**
     * 列表
     * @return mixed
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $get = $this->request->get();
            $list=PolicyAgreementLogic::lists($get);
            return json([
                'code' =>1,
                'msg'  =>'',
                'data' =>$list
            ]);
        }
        return $this->fetch();
    }

    /**
     * 添加文章
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $post['delete_time'] = 0;
            $result = $this->validate($post, 'app\admin\validate\PolicyAgreement.add');
            if ($result === true) {
                PolicyAgreementLogic::addArticle($post);
                return success('添加成功！');
            }
            return error($result);

        }
        return $this->fetch();
    }

    /**
     * 编辑文章
     */
    public function edit($id)
    {
        if ($this->request->isAjax()) {

            $post = $this->request->post();
            $post['delete_time'] = 0;
            $result = $this->validate($post, 'app\admin\validate\PolicyAgreement.edit');

            if ($result === true) {
                PolicyAgreementLogic::editArticle($post);
                return success('编辑成功！');
            }
            return error($result);

        }
        $article = PolicyAgreementLogic::getArticle($id);
        $this->assign('article', $article);
        return $this->fetch();
    }
}
